<template>
  <input type="text" placeholder="请输入员工姓名" v-model="emp.username">
  <input type="text" placeholder="请输入员工工资" v-model="emp.salary">
  <input type="text" placeholder="请输入员工岗位" v-model="emp.job">
  <br>
  <button @click="insertEmp()">点我添加</button>

  <table border="1px">
    <tr>
      <th>编号</th>
      <th>员工姓名</th>
      <th>员工工资</th>
      <th>员工岗位</th>
    </tr>
    <tr v-for="(emp,index) in arr">
      <td>{{index+1}}</td>
      <td>{{emp.username}}</td>
      <td>{{emp.salary}}</td>
      <td>{{emp.job}}</td>
    </tr>
  </table>
</template>

<script setup>
import {ref} from "vue";
//空数组用来存储数据
const arr=ref([]);
//空的对象用来绑定输入框
const emp=ref({
  username:'',
  salary:'',
  job:''
});

const insertEmp=()=> {
  //执行方法,创建对象接收数据
  let  e={
    username:emp.value.username,
    salary:emp.value.salary,
    job:emp.value.job
  }
  //把对象装进集合
  arr.value.push(e);
  //清空输入框,由于双向绑定,就是清空绑定的对象
  emp.value={
    username:'',
    salary:'',
    job:''
  }
}
</script>

<style scoped>

</style>